package InventoryAssetReceipt

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/client/inventory_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Add 添加资产入库的逻辑
func (l *AddLogic) Add(req *types.InventoryAssetReceipt) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置创建用户和更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，添加资产入库记录
	res, err := l.svcCtx.InventoryService.InventoryAssetReceiptAdd(l.ctx, &inventory_service.InventoryAssetReceipt{
		ReceiptCode:            req.ReceiptCode,            // 入库单号
		ReceiptDate:            req.ReceiptDate,            // 入库日期
		EquipmentSource:        req.EquipmentSource,        // 设备来源
		Receiver:               req.Receiver,               // 验收人
		ReceiverID:             req.ReceiverID,             // 验收人ID
		Warehouse:              req.Warehouse,              // 库房
		Supplier:               req.Supplier,               // 供应商
		SupplierID:             req.SupplierID,             // 供应商ID
		Purchaser:              req.Purchaser,              // 采购员
		PurchaserID:            req.PurchaserID,            // 采购员ID
		ContractNumber:         req.ContractNumber,         // 合同编号
		ContractID:             req.ContractID,             // 合同ID
		InvoiceNumber:          req.InvoiceNumber,          // 发票号码
		InvoiceDate:            req.InvoiceDate,            // 发票日期
		DepreciationMethod:     req.DepreciationMethod,     // 折旧方法
		DepreciationMethodCode: req.DepreciationMethodCode, // 折旧方法
		AssetUse:               req.AssetUse,               // 资产用途
		ReceiptMethod:          req.ReceiptMethod,          // 入库方式
		InstallationNumber:     req.InstallationNumber,     // 安装单号
		StatusID:               req.StatusID,               // 状态ID
		StatusName:             req.StatusName,             // 采购状态
		StockID:                req.StockID,                // 仓库ID
		TenantID:               tenantID,                   // 租户ID
		AssetUseCode:           req.AssetUseCode,           //资产用途Code
		ReceiptMethodCode:      req.ReceiptMethodCode,      //入库方式Code
		PurchaseID:             req.PurchaseID,             // 采购申请单ID
		PurchaseCode:           req.PurchaseCode,           // 采购編碼
		CreatedAtuser:          userIdStr,                  // 创建人
		UpdatedAtuser:          userIdStr,                  // 更新人
	})
	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	resp, err = result.Suc(res).End()
	//获取调用返回的id
	resp.Data = map[string]any{
		"id": res.Id,
	}
	// 返回成功的响应
	return resp, err
}
